perf: optimize array_replace for scalar needle#22387
Conversation
Jefffrey
left a comment
There was a problem hiding this comment.
One observation I have is this is a fast path for if from is a scalar, but it would be likely that to (and max too) might also be scalars in that case 🤔
Should we have the paths just be:
-- scalar fast path
select array_replace(array, scalar, scalar[, scalar]);
-- general fallback
select array_replace(array, scalar, array[, scalar]);
-- or any other combinationThoughts?
|
@Jefffrey Thanks for the review! I agree that to and max are also commonly scalar in practice and have already specialized them. but I think the performance gains here are relatively minor. The other suggestions have already been fixed. |
|
I was more thinking along the lines of explicitly expecting from/to/max to be scalars in the scalar path, instead of allowing them to be both scalar and array and having those code paths within the fast path. I guess what I'm trying to get at, is what behaviour is this optimization targeting? Like is there a common query pattern that this accelerates (where only |
|
I see your concern, all scalars is the most common pattern in practice, and scoping the optimization to just that case makes sense. Weighing in the code complexity of supporting those edge cases, I agree it's better to keep things simple — I'll narrow the fast path to all-scalars and let the general path handle the rest. |
|
run benchmark array_replace |
|
🤖 Criterion benchmark running (GKE) | trigger CPU Details (lscpu)Comparing perf/replace (7e348c2) to 4f45193 (merge-base) diff File an issue against this benchmark runner |
|
🤖 Criterion benchmark completed (GKE) | trigger Instance: CPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
run benchmark array_replace |
|
🤖 Criterion benchmark running (GKE) | trigger CPU Details (lscpu)Comparing perf/replace (7e348c2) to 4f45193 (merge-base) diff File an issue against this benchmark runner |
|
🤖 Criterion benchmark completed (GKE) | trigger Instance: CPU Details (lscpu)Details
Resource Usagebase (merge-base)
branch
File an issue against this benchmark runner |
|
All comments addressed, thanks for your review! @Jefffrey |
|
Thanks @lyne7-sc |
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Similar to apache#22387 (array_replace scalar optimization) `array_remove` / `array_remove_n` / `array_remove_all` perform element-wise comparison by invoking `compare_element_to_list` against each row's sub-array individually. When the needle is a scalar, this can be optimized by performing a single vectorized `distinct` comparison over the entire flattened values buffer. ## What changes are included in this PR? - Add a specialized removal kernel (`general_remove_with_scalar`) that uses `arrow_ord::cmp::distinct` with `Scalar` wrapper for a single bulk comparison pass over the flat values buffer. - Extend SLT tests with multi-row scalar-argument coverage, NULL-containing arrays, empty-array edge cases, boundary `n` values, and LargeList type coverage. <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ### Benchmarks ``` group main optimized ----- ---- --------- array_remove_all_int64/remove/list size: 10, num_rows: 4000 4.35 856.8±97.81µs ? ?/sec 1.00 196.9±4.48µs ? ?/sec array_remove_all_int64/remove/list size: 100, num_rows: 10000 1.90 5.5±0.09ms ? ?/sec 1.00 2.9±0.09ms ? ?/sec array_remove_all_int64/remove/list size: 500, num_rows: 10000 1.35 19.2±0.21ms ? ?/sec 1.00 14.2±0.48ms ? ?/sec array_remove_all_int64_nested/remove/list size: 10, num_rows: 4000 1.00 7.1±0.12ms ? ?/sec 1.04 7.4±0.12ms ? ?/sec array_remove_all_int64_nested/remove/list size: 100, num_rows: 3000 1.00 36.5±0.39ms ? ?/sec 1.05 38.3±2.61ms ? ?/sec array_remove_all_int64_nested/remove/list size: 300, num_rows: 1500 1.01 53.5±2.26ms ? ?/sec 1.00 53.0±0.99ms ? ?/sec array_remove_boolean/remove/list size: 10, num_rows: 4000 3.83 813.9±7.08µs ? ?/sec 1.00 212.4±2.28µs ? ?/sec array_remove_boolean/remove/list size: 100, num_rows: 10000 2.73 3.7±0.03ms ? ?/sec 1.00 1364.7±177.83µs ? ?/sec array_remove_boolean/remove/list size: 500, num_rows: 10000 2.34 9.8±0.14ms ? ?/sec 1.00 4.2±0.25ms ? ?/sec array_remove_fixed_size_binary/remove/list size: 10, num_rows: 4000 3.16 918.2±16.76µs ? ?/sec 1.00 290.6±9.79µs ? ?/sec array_remove_fixed_size_binary/remove/list size: 100, num_rows: 10000 1.56 6.9±0.13ms ? ?/sec 1.00 4.4±0.15ms ? ?/sec array_remove_fixed_size_binary/remove/list size: 500, num_rows: 10000 1.17 27.7±0.84ms ? ?/sec 1.00 23.6±2.04ms ? ?/sec array_remove_int64/remove/list size: 10, num_rows: 4000 4.55 825.7±6.30µs ? ?/sec 1.00 181.3±4.32µs ? ?/sec array_remove_int64/remove/list size: 100, num_rows: 10000 3.35 3.8±0.11ms ? ?/sec 1.00 1135.6±54.87µs ? ?/sec array_remove_int64/remove/list size: 500, num_rows: 10000 2.04 10.3±0.35ms ? ?/sec 1.00 5.1±0.39ms ? ?/sec array_remove_int64_nested/remove/list size: 10, num_rows: 4000 1.00 7.1±0.18ms ? ?/sec 1.02 7.2±0.07ms ? ?/sec array_remove_int64_nested/remove/list size: 100, num_rows: 3000 1.00 36.1±1.35ms ? ?/sec 1.07 38.5±3.67ms ? ?/sec array_remove_int64_nested/remove/list size: 300, num_rows: 1500 1.00 51.7±0.57ms ? ?/sec 1.05 54.1±2.13ms ? ?/sec array_remove_n_int64/remove/list size: 10, num_rows: 4000 4.43 845.3±5.00µs ? ?/sec 1.00 190.6±2.84µs ? ?/sec array_remove_n_int64/remove/list size: 100, num_rows: 10000 2.29 4.7±0.11ms ? ?/sec 1.00 2.0±0.12ms ? ?/sec array_remove_n_int64/remove/list size: 500, num_rows: 10000 1.63 14.8±0.42ms ? ?/sec 1.00 9.0±0.51ms ? ?/sec array_remove_n_int64_nested/remove/list size: 10, num_rows: 4000 1.00 7.0±0.09ms ? ?/sec 1.29 8.9±3.44ms ? ?/sec array_remove_n_int64_nested/remove/list size: 100, num_rows: 3000 1.00 36.6±0.42ms ? ?/sec 1.03 37.7±0.68ms ? ?/sec array_remove_n_int64_nested/remove/list size: 300, num_rows: 1500 1.00 52.7±3.68ms ? ?/sec 1.03 54.5±4.49ms ? ?/sec array_remove_strings/remove/list size: 10, num_rows: 4000 2.50 1144.6±21.95µs ? ?/sec 1.00 457.0±14.15µs ? ?/sec array_remove_strings/remove/list size: 100, num_rows: 10000 1.42 10.5±1.16ms ? ?/sec 1.00 7.4±0.34ms ? ?/sec array_remove_strings/remove/list size: 500, num_rows: 10000 1.12 39.8±0.91ms ? ?/sec 1.00 35.5±1.51ms ? ?/sec ``` ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes, existing and new SLT edge-case tests in `array_remove.slt`. ## Are there any user-facing changes? No. <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
Which issue does this PR close?
Rationale for this change
Currently,
array_replace/array_replace_n/array_replace_allperform element-wise comparison by invokingcompare_element_to_listagainst each row's sub-array individually. When the needle is a scalar, this can be optimized by performing a single vectorizednot_distinctcomparison over the entire flattened values buffer.What changes are included in this PR?
arrow_ord::cmp::not_distinctwithScalarwrapper for a single bulk comparison pass over the flat values buffer.nvalues for LargeList/FixedSizeList types.Benchmarks
Are these changes tested?
Yes, existing and new slt edge-case tests in
array_replace.slt.Are there any user-facing changes?
No.